package com.hyzx.qbasic.admin.controller.system;

import com.github.pagehelper.PageInfo;
import com.hyzx.qbasic.admin.model.constant.PermissionConst;
import com.hyzx.qbasic.admin.controller.BaseController;
import com.hyzx.qbasic.admin.model.annotation.SysLog;
import com.hyzx.qbasic.admin.model.dto.OrgTreeInfo;
import com.hyzx.qbasic.admin.model.po.OrgInfoPO;
import com.hyzx.qbasic.admin.model.po.OrgPO;
import com.hyzx.qbasic.admin.model.vo.req.admin.*;
import com.hyzx.qbasic.admin.model.vo.res.BaseOperationRes;
import com.hyzx.qbasic.admin.model.vo.res.BaseResModel;
import com.hyzx.qbasic.admin.common.util.BaseResUtils;
import com.hyzx.qbasic.admin.domain.service.IOrgService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.validation.Valid;
import java.util.List;

/**
 * Created by 黄江华 on 2017/9/29
 */


@Api(tags = "机构管理", description = "OrgController")
@Controller
@RequestMapping("/sys/org")
public class OrgController extends BaseController {

    @Autowired
    private IOrgService orgService;

    @ApiIgnore
    @SysLog("组织机构管理首页")
    @GetMapping("/list")
    public String list(Model model) {
        model.addAttribute("addFunc", getFunction(PermissionConst.SYS_ORG_ADD));
        model.addAttribute("editFunc", getFunction(PermissionConst.SYS_ORG_EDIT));
        model.addAttribute("deleteFunc", getFunction(PermissionConst.SYS_ORG_DELETE));
        return "system/org/list";
    }

    @ApiOperation("获取组织机构列表")
    @GetMapping("/getOrgList")
    @ResponseBody
    public BaseResModel<PageInfo<OrgInfoPO>> getOrgList(@Valid GetOrgListReq req, BindingResult result) {
        return BaseResUtils.success(orgService.getPagedOrgList(req));
    }

    @ApiIgnore
    @SysLog("新增组织机构页")
    @GetMapping("/add")
    public String add() {
        return "system/org/add";
    }

    @ApiOperation("新增组织机构表单提交")
    @PostMapping("/add")
    @ResponseBody
    public BaseResModel<BaseOperationRes> add(@Valid @RequestBody AddOrgReq req, BindingResult result) {
        BaseOperationRes res = orgService.addOrg(req);
        return BaseResUtils.success(res);
    }

    @ApiOperation("获取组织机构树列表")
    @GetMapping("/getOrgTreeList")
    @ResponseBody
    public BaseResModel<List<OrgTreeInfo>> getOrgTreeList() {
        return BaseResUtils.success(orgService.getOrgTreeList());
    }

    @ApiOperation("删除组织机构")
    @PostMapping("/delete")
    @ResponseBody
    public BaseResModel<BaseOperationRes> delete(@Valid @RequestBody DeleteOrgReq req, BindingResult result) {
        BaseOperationRes res = orgService.deleteOrg(req);
        return BaseResUtils.success(res);
    }

    @ApiIgnore
    @SysLog("编辑组织机构页")
    @GetMapping("/edit")
    public String edit() {
        return "system/org/edit";
    }

    @ApiOperation("编辑组织机构表单提交")
    @PostMapping("/edit")
    @ResponseBody
    public BaseResModel<BaseOperationRes> edit(@Valid @RequestBody EditOrgReq req, BindingResult result) {
        BaseOperationRes res = orgService.editOrg(req);
        return BaseResUtils.success(res);
    }

    @ApiOperation("获取组织机构信息")
    @GetMapping("/getOrgInfo")
    @ResponseBody
    public BaseResModel<OrgPO> getOrgInfo(@Valid GetOrgInfoReq req, BindingResult result) {
        return BaseResUtils.success(orgService.getOrgInfo(req.getOrgId()));
    }
}
